//
//  KFDSetAddressViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/18/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDSetAddressViewController.h"
#import "KFDUIConfigurator.h"
#import "UIPopoverListView.h"
#import "ui_config.h"
#import "KFDAddressbook.h"
#import "JTToast.h"
#import "KFDAccountManager.h"

#define UITableViewCellReuseIdentifier @"UITableViewCell"

@interface KFDSetAddressViewController ()<UIPopoverListViewDataSource,UIPopoverListViewDelegate>

@property (strong,nonatomic) KFDUserAddress* userAddress;

@property (strong,nonatomic) KFDAddressbook* addressbook;
@property (strong,nonatomic) KFDAddress* selectedProvince;
@property (strong,nonatomic) NSArray* cities;
@property (strong,nonatomic) KFDAddress* selectedCity;
@property (strong,nonatomic) NSArray* districts;
@property (strong,nonatomic) KFDAddress* selectedDistrict;

@property (strong,nonatomic) UIPopoverListView* provinceListView;
@property (strong,nonatomic) UIPopoverListView* cityListView;
@property (strong,nonatomic) UIPopoverListView* districtListView;
@property (weak, nonatomic) IBOutlet UILabel *provinceLabel;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *districtLabel;

@end

@implementation KFDSetAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"选择地区";
    self.userAddress=[[KFDUserAddress alloc] init];
    KFDAccountManager* accountManager=[KFDAccountManager sharedManager];
    KFDUserAddress* userAddress=accountManager.userAddress;
    NSString* province=userAddress.province;
    NSString* city=userAddress.city;
    NSString* district=userAddress.district;
    self.addressLabel.text=[NSString stringWithFormat:@"%@ %@ %@",province?province:@"",city?city:@"",district?district:@""];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.addressbook=[[KFDAddressbook alloc] init];
}

- (IBAction)saveButtonPressed:(id)sender {
    KFDAccountManager* accountManager=[KFDAccountManager sharedManager];
    if ([accountManager checkUserAddress:self.userAddress illegalInputHandler:^(NSString *message) {
        [[JTToast makeText:message] showInView:self.view size:CGSizeMake(20*message.length, 40)];
    }]) {
        [accountManager saveUserAddress:self.userAddress];
        [[JTToast makeText:@"保存成功"] showInView:self.view size:CGSizeMake(80, 40)];
    }
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(UIPopoverListView*)provinceListView
{
    if (!_provinceListView) {
        _provinceListView=[[UIPopoverListView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-40, SCREEN_HEIGHT*0.6)];
        [self setupPopoverListView:_provinceListView];
    }
    return _provinceListView;
}

-(UIPopoverListView*)cityListView
{
    if (!_cityListView) {
        _cityListView=[[UIPopoverListView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-40, SCREEN_HEIGHT*0.6)];
        [self setupPopoverListView:_cityListView];
    }
    return _cityListView;
}

-(UIPopoverListView*)districtListView
{
    if (!_districtListView) {
        _districtListView=[[UIPopoverListView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-40, SCREEN_HEIGHT*0.6)];
        [self setupPopoverListView:_districtListView];
    }
    return _districtListView;
}

-(void)setupPopoverListView:(UIPopoverListView*)popoverListView
{
    popoverListView.center=CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    popoverListView.datasource=self;
    popoverListView.delegate=self;
    [popoverListView.listView registerClass:[UITableViewCell class] forCellReuseIdentifier:UITableViewCellReuseIdentifier];
}

//----------------------------------------------------------------------------
#pragma mark - Table view delegate
//----------------------------------------------------------------------------
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 2:
        {
            [self.provinceListView show];
            break;
        }
        case 3:
        {
            if (self.selectedProvince) {
                [self.cityListView show];
            }else{
                [[JTToast makeText:@"请先选择省份"] showInView:self.view size:CGSizeMake(120,40)];
            }
            break;
        }
        case 4:
        {
            if (self.selectedCity) {
                [self.districtListView show];
            }else{
                [[JTToast makeText:@"请先选择城市"] showInView:self.view size:CGSizeMake(120,40)];
            }
            break;
        }
        default:
            break;
    }
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
#pragma mark - Popover list view data source
//----------------------------------------------------------------------------
-(NSInteger)popoverListView:(UIPopoverListView *)popoverListView numberOfRowsInSection:(NSInteger)section
{
    KFDAddressbook* addressbook=self.addressbook;
    if (popoverListView==self.provinceListView) {
        NSUInteger count=addressbook.provinces.count;
        return count;
//        return addressbook.provinces.count;
    }else if (popoverListView==self.cityListView){
        return [addressbook citiesInProvince:self.selectedProvince.number].count;
    }else if (popoverListView==self.districtListView){
        return [addressbook districtsInCity:self.selectedCity.number].count;
    }
    return 0;
}

-(UITableViewCell*)popoverListView:(UIPopoverListView *)popoverListView cellForIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell=[popoverListView.listView dequeueReusableCellWithIdentifier:UITableViewCellReuseIdentifier];
    if (!cell) {
        cell=[[UITableViewCell alloc] init];
    }
    if (popoverListView==self.provinceListView) {
        KFDAddress* province=self.addressbook.provinces[indexPath.row];
        cell.textLabel.text=province.name;
    }else if (popoverListView==self.cityListView){
        KFDAddress* city=self.cities[indexPath.row];
        cell.textLabel.text=city.name;
    }else if (popoverListView==self.districtListView){
        KFDAddress* district=self.districts[indexPath.row];
        cell.textLabel.text=district.name;
    }
    return cell;
}
//----------------------------------------------------------------------------


//----------------------------------------------------------------------------
#pragma mark - Popover list view delegate
//----------------------------------------------------------------------------
-(CGFloat)popoverListView:(UIPopoverListView *)popoverListView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(void)popoverListView:(UIPopoverListView *)popoverListView didSelectIndexPath:(NSIndexPath *)indexPath
{
    if (popoverListView==self.provinceListView) {
        KFDAddress* province=self.addressbook.provinces[indexPath.row];
        self.userAddress.province=province.name;
        self.selectedProvince=province;
        self.provinceLabel.text=province.name;
        self.cities=[self.addressbook citiesInProvince:province.number];
        self.cityLabel.text=@"城市";
        [self.cityListView.listView reloadData];
        self.addressLabel.text=[NSString stringWithFormat:@"中国 %@",province.name];
    }else if (popoverListView==self.cityListView){
        KFDAddress* city=self.cities[indexPath.row];
        self.userAddress.city=city.name;
        self.selectedCity=city;
        self.cityLabel.text=city.name;
        self.districts=[self.addressbook districtsInCity:city.number];
        self.districtLabel.text=@"区";
        [self.districtListView.listView reloadData];
        self.addressLabel.text=[NSString stringWithFormat:@"中国 %@ %@",self.selectedProvince.name,city.name];
    }else if (popoverListView==self.districtListView){
        KFDAddress* district=self.districts[indexPath.row];
        self.userAddress.district=district.name;
        self.selectedDistrict=district;
        self.districtLabel.text=district.name;
        self.addressLabel.text=[NSString stringWithFormat:@"中国 %@ %@ %@",self.selectedProvince.name,self.selectedCity.name,district.name];
    }
}
//----------------------------------------------------------------------------

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
